package teaManner;

import log.Logger;
import teacher.Teacher;

import java.io.*;
import java.util.*;

public class TeacherManager {
    private List<Teacher> teachers = new ArrayList<>();
    private Scanner sc = new Scanner(System.in);

    // 添加教师
    public void addTeacher() {
        String id = validateTeacherId(); // 验证教师ID
        System.out.print("输入教师姓名: ");
        String name = sc.nextLine();
        String gender = validateGender(); // 验证性别
        int age = validateAge(); // 验证年龄
        String subject = validateSubject(); // 验证学科

        Teacher teacher = new Teacher(id, name, gender, age, subject);
        teachers.add(teacher);
        System.out.println("教师添加成功！");
        Logger.log("教师添加成功");
    }

    // 查看所有教师
    public void viewTeachers() {
        if (teachers.isEmpty()) {
            System.out.println("没有教师信息！");
            Logger.log("没有教师信息");
            return;
        }
        System.out.println("教师列表：");
        teachers.forEach(System.out::println);
    }

    // 删除教师
    public void deleteTeacher() {
        System.out.print("输入要删除的教师ID: ");
        String id = sc.nextLine();
        boolean removed = teachers.removeIf(teacher -> teacher.getId().equals(id));
        if (removed) {
            System.out.println("教师删除成功！");
            Logger.log("教师删除成功");
        } else {
            System.out.println("未找到对应的教师 ID！");
            Logger.log("没找到对应教师ID");
        }
    }

    // 获取教师对象（根据ID）
    public Teacher getTeacherById(String id) {
        return teachers.stream().filter(t -> t.getId().equals(id)).findFirst().orElse(null);
    }

    // 保存到文件
    public void saveToFile() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("teachers.dat"))) {
            oos.writeObject(teachers);
            System.out.println("教师信息已保存到文件！");
            Logger.log("教师保存成功");
        } catch (IOException e) {
            System.out.println("保存失败: " + e.getMessage());
            Logger.log("保存失败");
        }
    }

    // 从文件加载
    public void loadFromFile() {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("teachers.dat"))) {
            teachers = (List<Teacher>) ois.readObject();
            System.out.println("教师信息已从文件加载！");
            Logger.log("教师信息加载成功");
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("加载失败: " + e.getMessage());
            Logger.log("加载失败");
        }
    }

    // 验证教师ID：必须以 AI 开头，长度为 6 位
    private String validateTeacherId() {
        while (true) {
            System.out.print("输入教师ID (以 AI 开头，6 位): ");
            String id = sc.nextLine();
            if (id.matches("^AI\\d{4}$")) {
                return id;
            }
            System.out.println("教师ID必须以 AI 开头，且为 6 位字符串，请重新输入！");
        }
    }

    // 验证性别：只能输入 male 或 female
    private String validateGender() {
        while (true) {
            System.out.print("输入性别 (male/female): ");
            String gender = sc.nextLine().toLowerCase();
            if (gender.equals("male") || gender.equals("female")) {
                return gender;
            }
            System.out.println("性别必须为 male 或 female，请重新输入！");
        }
    }

    // 验证年龄：限制在 30~70
    private int validateAge() {
        while (true) {
            System.out.print("输入年龄 (30~70): ");
            int age = sc.nextInt();
            sc.nextLine(); // 清除换行符
            if (age >= 30 && age <= 70) {
                return age;
            }
            System.out.println("年龄必须在 30~70 之间，请重新输入！");
        }
    }

    // 验证学科：只能输入常见科目
    private String validateSubject() {
        Set<String> validSubjects = Set.of("math", "english", "science", "history", "geography");
        while (true) {
            System.out.print("输入教授学科 (math/english/science/history/geography): ");
            String subject = sc.nextLine().toLowerCase();
            if (validSubjects.contains(subject)) {
                return subject;
            }
            System.out.println("学科必须是以下之一: math, english, science, history, geography，请重新输入！");
        }
    }
}
